local chengmo = fk.CreateSkill{
  name = "xiaobai__chengmo",
}

Fk:loadTranslationTable{
  ["xiaobai__chengmo"] = "诚谟",
  [":xiaobai__chengmo"] = "每轮限一次，其他角色于其回合内使用【杀】指定目标后，你可以交给任意名其他角色各一张手牌。"..
    "若如此做，其须选择一项："..
    "1.此【杀】结算完成后，本次因此获得牌的角色可以将一张手牌当【杀】或【过河拆桥】对其使用；"..
    "2.取消此【杀】所有目标。",
  ["#xiaobai__chengmo"] = "诚谟：你可以选择任意名角色和等量张张手牌，然后按选择顺序依次分配",
  ["#xiaobai__chengmo__1"] = "被使用【杀】或【过河拆桥】",
  ["#xiaobai__chengmo__2"] = "取消此【杀】所有目标。",
  ["#xiaobai__chengmo_use"] = "诚谟：你可以将一张手牌当【杀】或【过河拆桥】对 %dest 使用",
}

chengmo:addEffect(fk.AfterCardTargetDeclared, {
  can_trigger = function (self, event, target, player, data)
    return data.from ~= player and player:hasSkill(chengmo.name) and data.card.trueName == "slash"
      and player:getHandcardNum() > 0 and player.room.current == data.from 
      and player:usedSkillTimes(chengmo.name, Player.HistoryRound) < 1
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets,cards,done = room:askToChooseCardsAndPlayers(player, {
      equal = true,
      max_card_num = #room.alive_players - 1,
      max_num = #room.alive_players - 1,
      min_card_num = 1,
      min_num = 1,
      skill_name = chengmo.name,
      targets = room:getOtherPlayers(player, false),
      cancelable = true,
      prompt = "#xiaobai__chengmo"
    })
    local cost_data = {}
    for index = 1, #targets, 1 do
      cost_data[targets[index]] = cards[index]
    end
    if done then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local cost_data = event:getCostData(self)
    local room = player.room
    local targets = {}
    for _, p in ipairs(room.alive_players) do
      if cost_data[p] then
        room:obtainCard(p, cost_data[p], false, fk.ReasonGive)
        table.insert(targets, p)
      end
    end
    local choice = room:askToChoice(data.from, {
      choices = {"#xiaobai__chengmo__1", "#xiaobai__chengmo__2"},
      skill_name = chengmo.name,
    })
    if choice == "#xiaobai__chengmo__2" then
      data:removeAllTargets()
    else
      data.extra_data = data.extra_data or {}
      data.extra_data.xiaobai__chengmo_targets = targets
    end
  end
})

chengmo:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  mute = true,

  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(chengmo.name) and data.extra_data and data.extra_data.xiaobai__chengmo_targets and #data.extra_data.xiaobai__chengmo_targets > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local targets = data.extra_data.xiaobai__chengmo_targets ---@type ServerPlayer[]
    room:sortByAction(targets)
    for _, p in ipairs(targets) do
      room:askToUseVirtualCard(p, {
        name = {"slash", "dismantlement"},
        card_filter = {
          n = 1,
          cards = p:getCardIds(Player.Hand)
        },
        skill_name = chengmo.name,
        extra_data = {
          must_targets = {data.from.id},
          
        },
        prompt = "#xiaobai__chengmo_use::"..data.from.id,
      })
    end
  end
})

return chengmo
